﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;

namespace Algoritmos_de_Ordenação.Algoritmos
{
    public class ShellSort : DrawingRegion
    {
        public ShellSort()
        {
        }
        public ShellSort(TabItem tabItem)
            : base(tabItem, TabType.Shell)
        {
        }


        protected override void DoSort()
        {
            int i, j;
            int comparisons = 0;
            int swaps = 0;
            sorted = false;

            int h = 1, v;

            do
                h = 3 * h + 1;
            while (h < numElemArray);//7

            do
            {
                h /= 3;
                for (i = h; i < numElemArray; i++)//13
                {
                    v = numberArray[i];
                    j = i - h;

                    bool alteredJ = false;
                    while ((j >= 0) && (numberArray[j] > v))//19
                    {
                        alteredJ = true;
                        wf();
                        updateGBThread(18);
                        compareTwoWithFixedThread ( j+h, j, i, v );
                        {
                            wf ();
                            updateGBThread(20);
                            comparisons++;
                            swaps++;
                            numberArray[j + h] = numberArray[j];//21
                            compareTwoWithFixedThread(j + h, j, i, v);
                                                        
                            updateInformationsThread(SortStatus.Sorting, comparisons, swaps);

                            j -= h;
                        }
                    }
                    
                    wf();
                    updateGBThread(18);
                    if (alteredJ)
                    {
                        compareTwoWithFixedThread(j + h, j + h, i, v);
                    }
                    else compareTwoWithFixedThread(j + h, j, i, v);                    
                    
                    comparisons++;
                    wf ();
                    updateGBThread(24);
                    swaps++;
                    numberArray[j + h] = v;
                    compareTwoWithFixedThread(j + h, i, i, v);
                    updateInformationsThread(SortStatus.Sorting, comparisons, swaps);
                }
            } while (h > 1);//28
            endSorting(comparisons, swaps);
        }
    }
}
